<?php

if ( $argc <= 2 ) {
    print "Missing argument 'Batch Data Directory' or upload URL. Proper format is:\n\n";
    print "php $argv[0] [batch image directory path] [upload URL]\n\n";
    print "Example: php test/images http://www.daily-giggle.net/php/upload.php\n\n";
    exit(1);
}

$batchImageDir = $argv[1];
$uploadUrl = $argv[2];

if ($handle = opendir($argv[1])) {

    //Read subdirs
    while (false !== ($dir = readdir($handle))) {
		$fullPathToDir = $batchImageDir . '/' . $dir;
		if ( is_dir($fullPathToDir) && $dir != "." && $dir != ".." && $dir != ".svn") {

			//Process images in each subdir
			if ( $subHandle = opendir($fullPathToDir) ) {
			    while (false !== ($file = readdir($subHandle))) {
					if ( $file != "." && $file != ".." && $file != ".svn") {
						$fullPathToFile = $fullPathToDir . '/' . $file;
		    	   		print $fullPathToFile . "\n";
						uploadFile($uploadUrl, '__undefined__', $dir, '__undefined__', $fullPathToFile);
					}
				}
			}
			closedir($subHandle);
		
		}
    }

    closedir($handle);
}

function uploadFile($url, $description, $source, $email, $fileName){
	$docurl = curl_init(); 
	curl_setopt($docurl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($docurl, CURLOPT_URL, $url);
	curl_setopt($docurl, CURLOPT_VERBOSE, FALSE);
	curl_setopt($docurl, CURLOPT_HEADER, 1);
	curl_setopt($docurl, CURLOPT_POST, 1);
	curl_setopt($docurl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($docurl, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($docurl, CURLOPT_POSTFIELDS, array( 'fuf_description'=>$description,
							'fuf_source'=>$source,
							'fuf_email'=>$email,
							'fuf_file'=>'@' . $fileName, 'filename'=>'foobar.jpg'));
	$webpage = curl_exec($docurl);
	curl_close($docurl);
}

?>